import { FORM_CONFIG } from "../config/Config"
import { request, upload, myFetch } from "../utils/request"
import { checkResult } from "./init"
import { isNull } from "../utils/helper"
export default {
	namespace: 'form',
	state: {
	},
	subscriptions: {

	},
	effects: {
		*upload({ payload, type, action }, { call, put, select }) {
			let { callback } = payload;
			delete payload.callback;
			let result = yield call(upload, payload);
			if (checkResult(result)) {
				callback && callback(result)
			}
			else {
				callback && callback({ ...result, error: true })
			}
		},
		*query({ payload, type, action }, { call, put, select }) {
			yield put({ type: "global/setState", payload: { queryLoading: true } });
			const { apiParams, apiInfo, data, isEditCreate, errorFunc, formSuccessFunc, isShowModal, callback } = payload;
			let result = yield call(request, { apiParams, apiInfo, data, isEditCreate });
			if (checkResult(result)) {
				formSuccessFunc && formSuccessFunc(result, isShowModal);
				callback && callback(result);
				yield put({ type: "global/setState", payload: { queryLoading: false } })
			}
			else {
				errorFunc && errorFunc();
				callback && callback({ ...result, error: true });
				yield put({ type: "global/setState", payload: { queryLoading: false } })
			}
		},
		*getFC({ payload, type, action }, { call, put, select }) {
			const { callback, _c, formDataKey } = payload;
			let apiInfo = {
				url: FORM_CONFIG + "?_c=" + _c,
				page_enable: 0,
				params: []
			};
			let data = {};
			let result = yield call(request, { apiInfo, data });
			if (checkResult(result)) {
				yield put({
					type: "setStateByKey",
					payload: {
						key: formDataKey,
						value: {
							config: result.data
						}
					}
				});
				callback(result.data)
			}
			else {
				callback && callback({ ...result, error: true })
			}
		},
		*getInfo({ payload, type, action }, { call, put, select }) {
			const { callback, apiInfo, data } = payload;
			let result = yield call(request, { apiInfo, data });
			if (checkResult(result)) {
				callback(result.data)
			}
		},
		*getDataSource({ payload, type, action }, { call, put, select }) {
			let { apiInfo, data, dataset_key, options = [], source_type, formDataKey, pager, callback, tabs_id, component_type } = payload;
			yield put({ type: "setStateFd", payload: { formDataKey, value: { [dataset_key + "Loading"]: true } } });
			if (source_type == "server") {
				let result = yield call(myFetch, { apiInfo, data, pager });
				// 需要返回object的组件  如果组件需要object类型的数据，就拼接成object，否则拼接成数组
				let objectComponent = ",mi-chartcard,mi-html-card,mi-minipie,mi-miniprogress,mi-mini-subprogress,";
				if (checkResult(result, null, null, false)) {
					let resultData = result.data;
					if (!(resultData instanceof Array)) {// 某些组件的返回值时对象而不是数组
						options = {
							...options,
							...result.data
						}
					}
					else {
						try{
							// 返回数据为空时 result.data被默认处理为[]
							options = options.concat(result.data)
						}
						catch(e){
							console.log(e);
						}
					}
					// if(objectComponent.indexOf(","+component_type+",")!=-1){
					// 	options={
					// 		...options,
					// 		...result.data
					// 	}
					// }
					// else{
					// 	options=options.concat(result.data)
					// }
				}
			}
			if(component_type=="mi-listview"){
				if(source_type=="option"){
					callback && callback([{list:options}]);
				}
				else{
					callback && callback([options]);
				}
			}
			else{
				callback && callback(options);
			}
			yield put({ type: "setStateFd", payload: { formDataKey, value: { [dataset_key]: options } } });
			yield put({ type: "setStateFd", payload: { formDataKey, value: { [dataset_key + "Loading"]: false } } });
			if (!isNull(tabs_id)) {
				yield put({
					type: "setStateFd",
					payload: {
						formDataKey, value: { [tabs_id]: options[0].stat }
					}
				})
			}
		},
		*rulesApi({ payload, type, action }, { call, put, select }) {
			let { callback, apiInfo, apiParams, data } = payload;
			let result = yield call(request, { apiInfo, data, apiParams });
			if (checkResult(result, null, null, false)) {
			}
			else {
				result = {
					...result,
					error: true
				}
			}
			callback && callback(result)
		}
	},

	reducers: {
		setState(state, action) {
			return {
				...state, ...action.payload
			}
		},
		setStateByKey(state, { payload }) {
			let { key, value } = payload;
			let oldState = state[key];
			let obj = {
				[key]: {
					...oldState,
					...value
				}
			};
			return {
				...state,
				...obj
			};
		},
		setStateFd(state, action) {
			let { formDataKey, value } = action.payload;
			return {
				...state,
				[formDataKey]: {
					...state[formDataKey],
					...value
				}
			}
		},
		resetFormData(state, { payload }) {
			let { key } = payload;
			let data = state[key];
			return {
				...state,
				[key]: {
					...data,
					formData: {}
				}
			}
		},
		removeState(state, { payload }) {
			let { formDataKey } = payload;
			delete state[formDataKey];
			return {
				...state
			}
		},
		setFormData(state, action) {
			const { payload } = action;
			const { key, value, callback } = payload;
			callback && callback();
			if (state[key]) {
				return {
					...state,
					[key]: {
						...state[key],
						formData: {
							...state[key].formData,
							...value
						}
					}
				}
			}
			else {
				return {
					...state,
					[key]: {
						formData: {
							...value
						}
					}
				}
			}
		}
	}
};
